import { create } from 'zustand';
import { persist } from 'zustand/middleware';

// 【FIX】Update the UserInfo interface to use _id
interface UserInfo {
  _id: string; 
  username: string;
  email: string;
  role: string;
}

interface AuthState {
  userInfo: UserInfo | null;
  token: string | null;
  login: (userInfo: UserInfo, token: string) => void;
  logout: () => void;
}

const useAuthStore = create<AuthState>()(
  persist(
    (set) => ({
      userInfo: null,
      token: null,
      login: (userInfo, token) => set({ userInfo, token }),
      logout: () => set({ userInfo: null, token: null }),
    }),
    {
      name: 'auth-storage', 
    }
  )
);

export default useAuthStore;